package com

import kotlinx.serialization.KSerializer
import kotlinx.serialization.descriptors.PrimitiveKind
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
import kotlinx.serialization.encoding.Decoder
import kotlinx.serialization.encoding.Encoder
import java.nio.file.Path
import kotlin.io.path.Path
import kotlin.io.path.pathString

class PathSerializer : KSerializer<Path> {

    override val descriptor = PrimitiveSerialDescriptor("Path", PrimitiveKind.STRING)

    override fun serialize(encoder: Encoder, value: Path) {
        encoder.encodeString(value.pathString)
    }

    override fun deserialize(decoder: Decoder): Path {
        val decoderStr = decoder.decodeString()
        return runCatching {
            Path(decoderStr)
        }.onFailure {
            logger.error("Path 解析序列化错误 原数据:$decoderStr ", it)
        }.getOrThrow()

    }

}